﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace KaixinMobile.Forms
{
    using Kaixin;
    using Kaixin.Tools;
    using Kaixin.House.Garden;

    public partial class frmGarden : Form
    {
        private KaiXin001 ka = null;
        private MenuItem _LastChecked = null;
        private frmMain _Parent = null;

        public frmGarden(frmMain parent)
        {
            InitializeComponent();

            _Parent = parent;
            ka = parent.ka;
            UpdateFriendsList();
        }

        private void UpdateFriendsList()
        {
            miFriends.MenuItems.Clear();
            for (int i = 0; i < ka.FriendList.Items.Count; i++)
            {
                MenuItem mi = new MenuItem();
                mi.Text = ka.FriendList.Items[i].Name + " " + ka.FriendList.Items[i].Uid;
                mi.Click += new EventHandler(mi_Click);
                miFriends.MenuItems.Add(mi);
            }
        }

        private void mi_Click(object sender, EventArgs e)
        {
            string name = "";
            string fuid = "";

            Application.DoEvents();
            Cursor.Current = Cursors.WaitCursor;

            MenuItem mi = (sender as MenuItem);
            if (_LastChecked != null)
                _LastChecked.Checked = false;
            mi.Checked = true;
            _LastChecked = mi;

            if (Util.GetNameUId(mi.Text, out name, out fuid))
            {
                Garden garden = ka.GetGarden(fuid);
                lvGarden.Tag = garden;
                this.ShowGarden(garden);
            }

            Cursor.Current = Cursors.Default;
        }

        private void ShowGarden(Garden garden)
        {
            lvGarden.BeginUpdate();
            lvGarden.Items.Clear();
            for (int i = 0; i < garden.FarmList.Count; i++)
            {
                Farm farm = garden.FarmList[i];
                if (farm.Status == 1)
                {
                    ListViewItem item = new ListViewItem();

                    if (farm.CanHarvest())
                        item.ForeColor = Color.Red;

                    if (farm.CanPlough())
                        item.BackColor = Color.Gray;

                    item.Text = farm.FarmNum.ToString();
                    item.SubItems.Add(farm.Shared.ToString());
                    item.SubItems.Add(farm.Name);

                    if (farm.RemainNum > 0)
                        item.SubItems.Add(farm.FruitNum.ToString() + "/" + farm.RemainNum.ToString());
                    else
                        item.SubItems.Add(farm.FruitNum.ToString());

                    item.SubItems.Add(farm.Water.ToString() + "/" +
                        farm.Grass.ToString() + "/" +
                        farm.Vermin.ToString());

                    //item.SubItems.Add(farm.Thieved.ToString());

                    lvGarden.Items.Add(item);
                }
            }
            lvGarden.EndUpdate();
        }

        private void DoAction(Garden.ActionType action)
        {
            if (lvGarden.Tag != null)
            {
                Garden garden = (lvGarden.Tag as Garden);
                garden.DoAction(action);
            }
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            DoAction(Garden.ActionType.Harvest);
        }

        private void menuItem8_Click(object sender, EventArgs e)
        {
            DoAction(Garden.ActionType.Water);
        }

        private void menuItem9_Click(object sender, EventArgs e)
        {
            DoAction(Garden.ActionType.AntiGrass);
        }

        private void menuItem10_Click(object sender, EventArgs e)
        {
            DoAction(Garden.ActionType.AntiVermin);
        }

        private void menuItem11_Click(object sender, EventArgs e)
        {
            DoAction(Garden.ActionType.Plough);
        }

        private void menuItem12_Click(object sender, EventArgs e)
        {
            DoAction(Garden.ActionType.FarmSeedRandom);
        }

        private void miGardenAuto_Click(object sender, EventArgs e)
        {
            if (miGardenAuto.Checked)
            {
                miGardenAuto.Checked = false;
                ka.StopGardenAuto();
            }
            else
            {
                miGardenAuto.Checked = true;
                ka.GardenDoAuto();
            }
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            _Parent.lbResult.Parent = _Parent;
            Hide();
        }

        private void frmGarden_Activated(object sender, EventArgs e)
        {
            _Parent.lbResult.Parent = pnOutput;
        }
    }
}